﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;
using System.Data;
using BLL.Util;

namespace BLL
{
    public class CardBLL
    {
        /// <summary>
        /// 添加新账号,同时清除缓存中该账号的信息
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public static bool AddCard(Card card)
        {
            CacheHelper.RemoveCache(CacheHelper.MyCard, card.AID.ToString());
            CardDAL dal = new CardDAL();
            return dal.Insert(card);
        }
        /// <summary>
        /// 获取我所有账户集合from DB
        /// </summary>
        /// <param name="accountID">用户ID</param>
        /// <returns></returns>
        internal static List<Card> GetMyCardList(string accountID)
        {
            int aid = 0;
            if (!int.TryParse(accountID, out aid))
            {
                throw new ArgumentException();   
            }
            List<Card> list = new List<Card>();
            CardDAL dal = new CardDAL();
            var table = dal.GetCardsByAID(aid);
            foreach (DataRow item in table.Rows)
            {
                Card card = new Card()
                {
                    AID = aid,
                    CID = int.Parse(item["cid"].ToString()),
                    Title = item["title"].ToString(),
                    Money = decimal.Parse(item["Money"].ToString()),
                    Type = EnumHelper.GetEnumByValue<CardType>(typeof(CardType), item["type"].ToString()),
                    DueDate = DateTime.Parse(item["due_date"].ToString()),
                    BillDate = DateTime.Parse(item["bill_date"].ToString()),
                    CardNumber = item["cardNumber"] == null ? string.Empty : item["cardNumber"].ToString(),
                    CreditLine = decimal.Parse(item["credit_line"].ToString()),
                    Description = item["Description"].ToString(),
                    OrganizationID = int.Parse(item["oid"].ToString())
                };
                list.Add(card);
            }
            return list;            
        }
        /// <summary>
        /// 删除用户账户
        /// </summary>
        /// <param name="aid">用户ID</param>
        /// <param name="cid">账户ID</param>
        /// <returns></returns>
        public static bool DeleteCard(string aid, string cid)
        {
            var card = GetMyCard(aid, cid);
            CacheHelper.GetMyCard(aid).Remove(card);
            CardDAL dal = new CardDAL();
            return dal.Delete(cid);
        }
        /// <summary>
        /// 获取账户信息 from cache
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static Card GetMyCard(string aid, string cid)
        {
            var foo = from bar in CacheHelper.GetMyCard(aid) where bar.CID.ToString() == cid select bar;
            return foo.SingleOrDefault();
        }
        /// <summary>
        /// 编辑账户
        /// </summary>
        /// <param name="card"></param>
        public static void EditCard(Card card)
        {
            CardDAL dal = new CardDAL();
            dal.Update(card);
            var c = GetMyCard(card.AID.ToString(), card.CID.ToString());
            c.BillDate = card.BillDate;
            c.CardNumber = card.CardNumber;
            c.CreditLine = card.CreditLine;
            c.Description = card.Description;
            c.DueDate = card.DueDate;
            c.Money = card.Money;
            c.OrganizationID = card.OrganizationID;
            c.Title = card.Title;
            c.Type = card.Type;
        }
    }
}
